package com.zhu.esyblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhu.esyblog.Aop.Cache;
import com.zhu.esyblog.Aop.LogAnnotation;
import com.zhu.esyblog.pojo.Article;
import com.zhu.esyblog.pojo.PageBean;
import com.zhu.esyblog.pojo.RespBean;
import com.zhu.esyblog.service.IArticleService;
import com.zhu.esyblog.vo.ArticleParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stx
 * @since 2022-06-22
 */
@RestController
@RequestMapping("/articles")
@ResponseBody
@Api(tags = "文章模块")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    /**
     * 首页文章列表
     * @param pageBean
     * @return
     */
    @ApiOperation(value = "分页查询文章")
    @PostMapping
    @LogAnnotation(module="文章",operator="获取文章列表")
    @Cache(expire = 5*60*1000,name = "list_article")
    public RespBean ArticleList(@RequestBody PageBean pageBean){
//       int i=10/0;
       return articleService.listArticle(pageBean);
    }

    @ApiOperation(value = "测试分页查询")
    @PostMapping("/test")
    public RespBean ArticleLists(){
        QueryWrapper<Article>queryWrapper=new QueryWrapper<>();
        queryWrapper.like("title","spring");
        List<Article> list = articleService.list(queryWrapper);
        return RespBean.success("success",list);
    }
    @ApiOperation("最热文章")
    @PostMapping("/hot")
    @Cache(expire = 5*60*1000,name = "hot_article")
    public RespBean hotArticle(){
       int limit=5;
       return  articleService.hotArticle(limit);
    }
    @ApiOperation("最新文章")
    @PostMapping("/new")
    @Cache(expire = 5*60*1000,name = "news_article")
    public RespBean newArticles(){
        int limit=5;
        return  articleService.newArticles(limit);
    }
    @ApiOperation("文章归档")
    @PostMapping("/listArchives")
    public RespBean listArchives(){

        return  articleService.listArchives();
    }
    @ApiOperation(value = "发布文章")
    @PostMapping("/publish")
    public RespBean publish(@RequestBody ArticleParam articleParam){
        return articleService.publish(articleParam);

    }

}
